<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layuiAdmin 文章管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 30px 0 0;">
    <input type="hidden" name="id" value="{$find.id}">
    <div class="layui-form-item">
        <label class="layui-form-label">区块名称</label>
        <div class="layui-input-inline" style="width: 250px;">
            <input type="text" class="layui-input" name="name" value="{$find.name}" placeholder="区块名称" autocomplete="off">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">背景图片</label>
        <div class="layui-input-inline">
            <input type="text"  name="bg" value="{$find.bg}" placeholder="背景图片" autocomplete="off" class="layui-input">
        </div>
        <button style="float: left;" type="button" class="layui-btn" id="demo">上传图片</button>
        <!--<div style="float: left;font-size: 12px;line-height: 38px;margin-left: 16px;">(375 x 240)</div>-->
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">图片链接</label>
        <div class="layui-input-inline">
            <input type="text" name="link" value="{$find.link}" layadmin-event="page"  placeholder="链接" autocomplete="off" class="layui-input" >
            <input type="hidden" name="link_column" value="{$find.link_column}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示方法</label>
        <div class="layui-input-block">
            <input type="radio" name="type" value="1" lay-filter="type" title="一行1条" {if condition="$find eq ''"}checked{/if}{eq name="find.type" value="1"}checked{/eq}>
            <input type="radio" name="type" value="2" lay-filter="type" title="一行2条" {eq name="find.type" value="2"}checked{/eq}>
            <input type="radio" name="type" value="3" lay-filter="type" title="一行3条" {eq name="find.type" value="3"}checked{/eq}>
            <input type="radio" name="type" value="4" lay-filter="type" title="横向滚动" {eq name="find.type" value="4"}checked{/eq}>
            <input type="radio" name="type" value="5" lay-filter="type" title="广告滚动" {eq name="find.type" value="5"}checked{/eq}>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">调用数据</label>
        <div class="layui-input-block">
            <input type="radio" class="notAd" name="model" value="1" title="推荐" {if condition="$find eq ''"}checked{/if}{eq name="find.model" value="1"}checked{/eq}{if condition='$find.type eq 5'}disabled{/if}>
            <input type="radio" class="notAd" name="model" value="2" title="精品" {eq name="find.model" value="2"}checked{/eq}{if condition='$find.type eq 5'}disabled{/if}>
            <input type="radio" class="notAd" name="model" value="3" title="新品" {eq name="find.model" value="3"}checked{/eq}{if condition='$find.type eq 5'}disabled{/if}>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">显示条数</label>
        <div class="layui-input-inline">
            <input type="text" name="num" value="{$find.num}" lay-verify="required|number" placeholder="显示条数" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序</label>
        <div class="layui-input-inline">
            <input type="text" name="sort" value="{$find.sort}" lay-verify="required|number" placeholder="请输入排序" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">是否显示</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="是" {if condition="$find eq ''"}checked{/if}{eq name="find.status" value="1"}checked{/eq}>
            <input type="radio" name="status" value="0" title="否" {eq name="find.status" value="0"}checked{/eq}>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="确认添加">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-edit" id="layuiadmin-app-form-edit" value="确认编辑">
    </div>
    <script type="text/html" id="time">
        {{ formatDateTime(d.time) }}
    </script>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload', 'tableSelect'], function(){
        var $ = layui.$,
            form = layui.form,
            upload = layui.upload,
            tableSelect = layui.tableSelect;

        tableSelect.render({
            elem: '#link',	//定义输入框input对象
            checkedKey: 'id', //表格的唯一建值，非常重要，影响到选中状态 必填
            searchKey: 'name',	//搜索输入框的name值 默认keyword
            searchPlaceholder: '搜索商品名称',	//搜索输入框的提示文字 默认关键词搜索
            table: {	//定义表格参数，与LAYUI的TABLE模块一致，只是无需再定义表格elem
                url: "{:url('Product/product')}",
                method: 'post',
                cols: [[
                    {type: 'radio'},
                    {field: 'name', title: '商品名称', width: 400},
                    {field: 'stock', title: '库存', width: 100},
                    {field: 'volume', title: '销量', width: 100},
                    {field: 'time', title: '添加时间', width: 200, templet: "#time"}
                ]]
            },
            done: function (elem, data) {
                //选择完后的回调，包含2个返回值 elem:返回之前input对象；data:表格返回的选中的数据 []
                //拿到data[]后 就按照业务需求做想做的事情啦~比如加个隐藏域放ID...
                var field = data.data;
                $("input[name=link]").val(field[0].id);
                $("#link").val(field[0].name);
            }
        });

        form.on('radio(type)', function(data){
            if (data.value == 5){
                $(".notAd").prop('disabled', true);
                $(".Ad").prop('checked', true);
            }else{
                $(".notAd").prop('disabled', false);
            }
            form.render();
        });

        //普通图片上传
        upload.render({
            elem: '#demo'
            ,url: "{:url('System/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg(res.msg);
                }
                //上传成功
                $("input[name=bg]").val(res.img);
                layer.msg('上传成功');
            }
        });

        //图片上传
        upload.render({
            elem: '#demo2'
            ,url: "{:url('System/upload')}"
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    return layer.msg(res.msg);
                }
                //上传成功
                $("input[name=wan]").val(res.img);
                layer.msg('上传成功');
            }
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-submit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('System/foot_add')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('添加成功');
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('添加失败');
                    }
                }
            });
        });

        //监听提交
        form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                type: "POST",
                url: "{:url('System/foot_edit')}",
                data: {field:field},
                dataType: "json",
                success: function(e){
                    if (e == 1){
                        layer.msg('修改成功');
                        setTimeout(function () {
                            parent.layui.table.reload('demo'); //重载表格
                            parent.layer.close(index); //再执行关闭
                        }, 500)
                    }else{
                        layer.msg('修改失败');
                    }
                }
            });
        });
    })

    //php时间戳用js转换为yyyy-mm-dd
    function formatDateTime(timeStamp) {
        var date = new Date();
        date.setTime(timeStamp * 1000);
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? ('0' + m) : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        var h = date.getHours();
        h = h < 10 ? ('0' + h) : h;
        var minute = date.getMinutes();
        var second = date.getSeconds();
        minute = minute < 10 ? ('0' + minute) : minute;
        second = second < 10 ? ('0' + second) : second;
        return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
    }
</script>
</body>
</html>